package com.tjlg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjlg.auto.JSONAuthentication;
import com.tjlg.entity.WaTask;
import com.tjlg.entity.WaUser;
import com.tjlg.mapper.WaLxMapper;
import com.tjlg.mapper.WaTaskMapper;
import com.tjlg.mapper.WaUserMapper;
import com.tjlg.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tjlg
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/wa-task")
public class WaTaskController extends JSONAuthentication {
    @Autowired
    WaTaskMapper waTaskMapper;
    @Autowired
    WaLxMapper waLxMapper;
    @Autowired
    WaUserMapper waUserMapper;
    @ApiOperation(value = "添加巡逻任务")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody WaTask waTask) throws  Exception{
       if(waTaskMapper.insert(waTask)>0){
           Result ok = Result.ok().message("安排成功");
           this.WriteJSON(request, response, ok);
       }else{
           Result error = Result.error();
           this.WriteJSON(request, response, error);
       }
    }
    @ApiOperation(value = "删除任务")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=waTaskMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改巡逻任务状态（已接取）")
    @PostMapping("/updJq")
    public void updJq(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        WaTask waTask = waTaskMapper.selectById(id);
        waTask.setStatus("已接取");
        if(waTaskMapper.updateById(waTask)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改巡逻任务状态（已完成）")
    @PostMapping("/updWc")
    public void updWc(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        WaTask waTask = waTaskMapper.selectById(id);
        waTask.setStatus("已完成");
        if(waTaskMapper.updateById(waTask)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有待确定的任务")
    @PostMapping("/findAllDjq")
    public void findAllDjq(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","待确定");
        Page<WaTask> shUserPage=new Page<>(current,size);
        Page<WaTask> shUserPages=waTaskMapper.selectPage(shUserPage,wrapper);
        List<WaTask> records = shUserPages.getRecords();
        int total=waTaskMapper.selectCount(wrapper);
        for (WaTask a :records){
        a.setWaUser(waUserMapper.selectById(a.getUserId()));
        a.setWaLx(waLxMapper.selectById(a.getLxId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有已接取的任务")
    @PostMapping("/findAllYjq")
    public void findAllYjq(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已接取");
        Page<WaTask> shUserPage=new Page<>(current,size);
        Page<WaTask> shUserPages=waTaskMapper.selectPage(shUserPage,wrapper);
        List<WaTask> records = shUserPages.getRecords();
        int total=waTaskMapper.selectCount(wrapper);
        for (WaTask a :records){
            a.setWaUser(waUserMapper.selectById(a.getUserId()));
            a.setWaLx(waLxMapper.selectById(a.getLxId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有已完成任务")
    @PostMapping("/findAllWc")
    public void findAllWc(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已完成");
        Page<WaTask> shUserPage=new Page<>(current,size);
        Page<WaTask> shUserPages=waTaskMapper.selectPage(shUserPage,wrapper);
        List<WaTask> records = shUserPages.getRecords();
        int total=waTaskMapper.selectCount(wrapper);
        for (WaTask a :records){
            a.setWaUser(waUserMapper.selectById(a.getUserId()));
            a.setWaLx(waLxMapper.selectById(a.getLxId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有已完成任务根据用户id")
    @PostMapping("/findAllWcByid")
    public void findAllWc(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已完成");
        wrapper.eq("user_id",userId);
        Page<WaTask> shUserPage=new Page<>(current,size);
        Page<WaTask> shUserPages=waTaskMapper.selectPage(shUserPage,wrapper);
        List<WaTask> records = shUserPages.getRecords();
        int total=waTaskMapper.selectCount(wrapper);
        for (WaTask a :records){
            a.setWaUser(waUserMapper.selectById(a.getUserId()));
            a.setWaLx(waLxMapper.selectById(a.getLxId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有待完成任务根据用户id")
    @PostMapping("/findAllJqByid")
    public void findAllJqByid(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已接取");
        wrapper.eq("user_id",userId);
        Page<WaTask> shUserPage=new Page<>(current,size);
        Page<WaTask> shUserPages=waTaskMapper.selectPage(shUserPage,wrapper);
        List<WaTask> records = shUserPages.getRecords();
        int total=waTaskMapper.selectCount(wrapper);
        for (WaTask a :records){
            a.setWaUser(waUserMapper.selectById(a.getUserId()));
            a.setWaLx(waLxMapper.selectById(a.getLxId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有待确定任务根据用户id")
    @PostMapping("/findAllDqrByid")
    public void findAllDqrByid(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","待确定");
        wrapper.eq("user_id",userId);
        Page<WaTask> shUserPage=new Page<>(current,size);
        Page<WaTask> shUserPages=waTaskMapper.selectPage(shUserPage,wrapper);
        List<WaTask> records = shUserPages.getRecords();
        int total=waTaskMapper.selectCount(wrapper);
        for (WaTask a :records){
            a.setWaUser(waUserMapper.selectById(a.getUserId()));
            a.setWaLx(waLxMapper.selectById(a.getLxId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

